package com.lcz.authority.admin.mapper;

import cn.hutool.core.util.ObjectUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.LambdaQueryWrapperX;
import com.lcz.authority.common.core.mybatis.MPJLambdaWrapperX;
import com.lcz.authority.common.domain.BaseEntity;
import com.lcz.authority.common.domain.entity.*;
import com.lcz.authority.common.domain.model.SysRoleAuth;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lcz
 * @desc 菜单映射器
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@Mapper
public interface SysMenuMapper extends BaseMapperX<SysMenuEntity> {

    /**
     * 查询系统菜单列表
     *
     * @param menu 菜单信息
     * @return 菜单列表
     */
    default List<SysMenuEntity> selectMenuList(SysMenuEntity menu) {

        return selectList(new LambdaQueryWrapperX<SysMenuEntity>()
                .like(ObjectUtil.isNotNull(menu.getMenuName()), SysMenuEntity::getMenuName, menu.getMenuName())
                .eq(ObjectUtil.isNotNull(menu.getVisible()), SysMenuEntity::getVisible, menu.getVisible())
                .eq(ObjectUtil.isNotNull(menu.getStatus()), SysMenuEntity::getStatus, menu.getStatus()));
    }

    /**
     * 根据用户查询系统菜单列表
     *
     * @param menu 菜单信息
     * @return 菜单列表
     */
    default List<SysMenuEntity> selectMenuListByUserId(SysMenuEntity menu) {
        //select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status,
        //        ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
        //        from sys_menu m
        //        left join sys_role_menu rm on m.menu_id = rm.menu_id
        //        left join sys_user_role ur on rm.role_id = ur.role_id
        //        left join sys_role ro on ur.role_id = ro.role_id
        //        where ur.user_id = #{params.userId}
        //        and m.del_flag = '0'
        //        and ro.del_flag = '0'
        //        <if test="menuName != null and menuName != ''">
        //            AND m.menu_name like concat('%', #{menuName}, '%')
        //        </if>
        //        <if test="visible != null and visible != ''">
        //            AND m.visible = #{visible}
        //        </if>
        //        <if test="status != null and status != ''">
        //            AND m.status = #{status}
        //        </if>
        //        order by m.parent_id, m.order_num
        MPJLambdaWrapperX<SysMenuEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysMenuEntity.class);
        wrapperX.leftJoin(SysMenuEntity.class, "menu_id", SysMenuEntity::getMenuId, SysRoleMenuEntity::getMenuId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, "role_id", SysRoleMenuEntity::getRoleId, SysUserRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserRoleEntity.class, "user_id", SysUserRoleEntity::getUserId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysRoleEntity.class, "role_id", SysRoleEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.eq(SysUserRoleEntity::getUserId, menu.getParams());
        wrapperX.eq(SysRoleEntity::getDelFlag, 0);
        wrapperX.eq(SysRoleEntity::getDelFlag, 0);
        wrapperX.likeIfPresent(SysMenuEntity::getMenuName, menu.getMenuName());
        wrapperX.eqIfPresent(SysMenuEntity::getVisible, menu.getVisible());
        wrapperX.eqIfPresent(SysMenuEntity::getStatus, menu.getStatus());
        wrapperX.orderByAsc(SysMenuEntity::getParentId, SysMenuEntity::getOrderNum);
        return selectList(wrapperX);
    }

    /**
     * 根据用户ID查询菜单
     *
     * @return 菜单列表
     */
    default List<SysMenuEntity> selectMenuTreeAll() {
        // select distinct m.menu_id,
        //                        m.parent_id,
        //                        m.menu_name,
        //                        m.path,
        //                        m.component,
        //                        m.`query`,
        //                        m.visible,
        //                        m.status,
        //                        ifnull(m.perms, '') as perms,
        //                        m.is_frame,
        //                        m.is_cache,
        //                        m.menu_type,
        //                        m.icon,
        //                        m.order_num,
        //                        m.create_time
        //        from sys_menu m
        //        where m.menu_type in ('M', 'C')
        //          and m.status = 0
        //          and m.del_flag = '0'
        //        order by m.parent_id, m.order_num
        MPJLambdaWrapperX<SysMenuEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysMenuEntity.class);
        wrapperX.in(SysMenuEntity::getMenuType, "M", "C");
        wrapperX.eq(SysMenuEntity::getStatus, "0");
        wrapperX.eq(SysMenuEntity::getDelFlag, "0");
        wrapperX.orderByAsc(SysMenuEntity::getParentId, SysMenuEntity::getOrderNum);
        return selectList(wrapperX);
    }

    /**
     * 根据用户ID查询菜单
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    default List<SysMenuEntity> selectMenuTreeByUserId(String userId) {
        // select distinct m.menu_id,
        //                        m.parent_id,
        //                        m.menu_name,
        //                        m.path,
        //                        m.component,
        //                        m.`query`,
        //                        m.visible,
        //                        m.status,
        //                        ifnull(m.perms, '') as perms,
        //                        m.is_frame,
        //                        m.is_cache,
        //                        m.menu_type,
        //                        m.icon,
        //                        m.order_num,
        //                        m.create_time
        //        from sys_menu m
        //                 left join sys_role_menu rm on m.menu_id = rm.menu_id
        //                 left join sys_user_role ur on rm.role_id = ur.role_id
        //                 left join sys_role ro on ur.role_id = ro.role_id
        //                 left join sys_user u on ur.user_id = u.user_id
        //        where u.user_id = #{userId}
        //          and m.menu_type in ('M', 'C')
        //          and m.status = 0
        //          and m.del_flag = '0'
        //          AND ro.status = 0
        //          and ro.del_flag = '0'
        //        order by m.parent_id, m.order_num
        MPJLambdaWrapperX<SysMenuEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysMenuEntity.class);
        wrapperX.leftJoin(SysMenuEntity.class, "resource_code", SysMenuEntity::getMenuId, SysRoleMenuEntity::getMenuId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, "role_id", SysRoleMenuEntity::getRoleId, SysUserRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserRoleEntity.class, "role_id", SysUserRoleEntity::getRoleId, SysRoleMenuEntity::getRoleId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, "role_id", SysRoleMenuEntity::getRoleId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysRoleEntity.class, "role_id", SysRoleEntity::getRoleId, SysUserRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, "user_id", SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysUserEntity::getUserId, userId);
        wrapperX.in(SysMenuEntity::getMenuType, "M", "C");
        wrapperX.eq(SysMenuEntity::getStatus, "0");
        wrapperX.eq(SysMenuEntity::getDelFlag, "0");
        wrapperX.eq(SysRoleEntity::getStatus, "0");
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.orderByAsc(SysMenuEntity::getParentId, SysMenuEntity::getOrderNum);
        return selectList(wrapperX);
    }


    /**
     * 根据角色ID查询权限
     *
     * @param roleId 角色ID
     * @return 权限列表
     */
    default List<String> selectMenuPermsByRoleId(String roleId) {
        //<select id="selectMenuPermsByRoleId" parameterType="String" resultType="String">
        //        select distinct m.perms
        //        from sys_menu m
        //                 left join sys_role_menu rm on m.menu_id = rm.menu_id
        //        where m.status = '0'
        //          and m.del_flag = '0'
        //          and rm.role_id = #{roleId}
        //    </select>
        MPJLambdaWrapperX<SysMenuEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.select(SysMenuEntity::getPerms);
        wrapperX.eq(SysMenuEntity::getStatus, "0");
        wrapperX.eq(SysMenuEntity::getDelFlag, "0");
        wrapperX.in(SysRoleMenuEntity::getRoleId, roleId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, "menu_id", SysRoleMenuEntity::getMenuId, SysMenuEntity::getMenuId);
        return selectList(wrapperX).stream().map(SysMenuEntity::getPerms).collect(Collectors.toList());
    }

    /**
     * 查询所有角色的权限列表
     *
     * @return SysRolePerms
     */
    default List<SysRoleAuth> getMenuPermsAll() {
        //    <select id="getMenuPermsAll" resultMap="RoleAuthResult">
        //        select m.perms, rm.role_id
        //        from sys_menu m
        //                 left join sys_role_menu rm on m.menu_id = rm.menu_id
        //        where m.status = '0'
        //          AND m.del_flag = '0'
        //          AND rm.role_id IS NOT NULL
        //    </select>
        MPJLambdaWrapper<SysMenuEntity> wrapperX = new MPJLambdaWrapper<>();
        wrapperX.selectAs(SysMenuEntity::getPerms, SysRoleAuth::getPerms).selectAs(SysRoleMenuEntity::getRoleId, SysRoleAuth::getRoleId);
        wrapperX.eq(SysMenuEntity::getStatus, "0");
        wrapperX.eq(SysMenuEntity::getDelFlag, "0");
        wrapperX.isNotNull(SysRoleMenuEntity::getRoleId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, "menu_id", SysRoleMenuEntity::getMenuId, SysMenuEntity::getMenuId);
        return selectJoinList(SysRoleAuth.class, wrapperX);
    }

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    default List<String> selectMenuPermsByUserId(String userId) {
        //        select distinct m.perms
        //        from sys_menu m
        //                 left join sys_role_menu rm on m.menu_id = rm.menu_id
        //                 left join sys_user_role ur on rm.role_id = ur.role_id
        //                 left join sys_role r on r.role_id = ur.role_id
        //        where m.status = '0'
        //          and r.status = '0'
        //          and r.del_flag = '0'
        //          and m.del_flag = '0'
        //          and ur.user_id = #{userId}
        MPJLambdaWrapperX<SysMenuEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.select(SysMenuEntity::getPerms);
        wrapperX.eq(SysMenuEntity::getStatus, "0");
        wrapperX.eq(SysRoleEntity::getStatus, "0");
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eq(SysMenuEntity::getDelFlag, "0");
        wrapperX.eq(SysUserRoleEntity::getUserId, userId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, "menu_id", SysRoleMenuEntity::getMenuId, SysMenuEntity::getMenuId);
        wrapperX.leftJoin(SysUserRoleEntity.class, "role_id", SysUserRoleEntity::getRoleId, SysRoleMenuEntity::getRoleId);
        wrapperX.leftJoin(SysRoleEntity.class, "role_id", SysRoleEntity::getRoleId, SysUserRoleEntity::getRoleId);
        return selectList(wrapperX).stream().map(SysMenuEntity::getPerms).collect(Collectors.toList());
    }


    /**
     * 根据角色ID查询菜单树信息
     *
     * @param roleId            角色ID
     * @param menuCheckStrictly 菜单树选择项是否关联显示
     * @return 选中菜单列表
     */
    default List<String> selectMenuListByRoleId(@Param("roleId") String roleId, @Param("menuCheckStrictly") boolean menuCheckStrictly) {
        MPJLambdaWrapper<SysMenuEntity> wrapperX = new MPJLambdaWrapper<>();
        wrapperX.select(SysMenuEntity::getMenuId);
        wrapperX.leftJoin(SysRoleMenuEntity.class, SysRoleMenuEntity::getMenuId, SysMenuEntity::getMenuId);
        wrapperX.eq(SysRoleMenuEntity::getRoleId, roleId);
        wrapperX.eq(SysMenuEntity::getDelFlag, BaseEntity.IsDelFlag.NO);
        wrapperX.orderByAsc(SysMenuEntity::getParentId, SysMenuEntity::getOrderNum);
        List<String> collect = selectList(wrapperX).stream().map(SysMenuEntity::getMenuId).collect(Collectors.toList());
        if (menuCheckStrictly) {
            MPJLambdaWrapper<SysMenuEntity> wrapper = new MPJLambdaWrapper<>();
            wrapper.select(SysMenuEntity::getParentId).innerJoin(SysRoleMenuEntity.class, SysRoleMenuEntity::getMenuId, SysMenuEntity::getMenuId).eq(SysRoleMenuEntity::getRoleId, roleId);
            List<String> collect1 = selectList(wrapper).stream().map(SysMenuEntity::getParentId).collect(Collectors.toList());
            collect.removeAll(collect1);

        }
        return collect;
    }

    /**
     * 校验菜单名称是否唯一
     */
    default SysMenuEntity checkMenuNameUnique(SysMenuEntity menu) {
        return selectOne(new LambdaQueryWrapperX<SysMenuEntity>()
                .eq(SysMenuEntity::getMenuName, menu.getMenuName())
                .eq(SysMenuEntity::getParentId, menu.getParentId())
        );
    }


}
